<template>
  <div>
    <FormPanel ref="formPanel" :list="formList" :formType="type" @formChange="changeHandle" :isSubmitBtn="false" :collapse="false" labelWidth="110" />

    <div
      :style="{
        position: 'absolute',
        left: 0,
        bottom: 0,
        right: 0,
        zIndex: 9,
        borderTop: '1px solid #e9e9e9',
        padding: '10px 20px',
        background: '#fff',
        textAlign: 'center'
      }"
    >
      <el-button @click="closeDrawer(1)">返 回</el-button>
      <multiuse-button :click="closeDrawer.bind(this, 2)" type="primary" v-if="type != 'show'">入库确认</multiuse-button>
    </div>
  </div>
</template>
<script>
import moment from 'moment';
import { getUser } from '@/assets/js/auth';
import { queryReturnById, saveSalesReturn, getWarehouseList } from '@/api/dealerSale/SalesReturn/ReturnVehicle';

import { dictionary } from '@/utils/dictMixin';
export default {
  mixins: [dictionary],
  data() {
    this.saveData = null;
    return {
      formList: this.createdFrom(),
      issearch: false,
      carData: {},
      detailData: {},
      vehicleData: {
        reportId: '',
        vehicleId: ''
      }
    };
  },
  components: {},
  props: ['type', 'rowData'],
  created() {
    this.init();
    this.getList();
  },
  methods: {
    init() {
      if (this.type == 'emit') {
        // 编辑
        this.toggleFun(2);
        this.getDetail(this.rowData.returnId);
      } else if (this.type == 'show') {
        // 详情
        this.toggleFun(1);
        this.getDetail(this.rowData.returnId);
      } else {
        // 新增
        this.toggleFun(2);
      }
    },
    async getList() {
      let res = await getWarehouseList();
      this.formList.find(x => x.fieldName == 'returnWarehouse').itemList = res.data.list.map(x => ({ text: x.warehouseName, value: x.warehouseId }));
    },
    toggleFun(type) {
      if (type == 1) {
        // 详情
        this.formList.map(x => {
          x.disabled = true;
        });
      }
    },
    async getDetail(id) {
      let res = await queryReturnById({ returnId: id });
      this.detailData = res.data;

      this.vehicleData.vehicleId = res.data.vehicleId;
      this.vehicleData.reportId = res.data.reportId;
      this.carData.dealerCode = res.data.dealerCode;
      this.carData.dealerName = res.data.dealerName;
      if (res.data.otherUrl) {
        res.data.fj = [{ name: res.data.otherFileName, url: res.data.otherUrl }];
      }
      for (let i in res.data) {
        this.formList.map(x => {
          if (x.fieldName == i) {
            x.initialValue = res.data[i];
          }
        });
      }
    },
    seacherFun() {
      this.issearch = true;
    },
    changeHandle(val) {
      this.saveData = val;
    },
    emitcloseHandler(val) {
      if (val && val.length > 0) {
        this.carData = val[0];
        for (let i in this.carData) {
          this.formList.map(x => {
            if (x.fieldName == i) {
              x.initialValue = this.carData[i];
            }
          });
        }
        this.vehicleData.vehicleId = val[0].vehicleId;
        this.vehicleData.reportId = val[0].reportId;
      }
      this.issearch = false;
    },
    async closeDrawer(type) {
      if (type == 1) {
        this.$emit('close', false, type);
      } else {
        this.$refs.formPanel.SUBMIT_FORM();
        if (this.saveData != null) {
          let saveData = this.saveData;
          if (this.type == 'emit') {
            saveData.returnId = this.rowData.returnId;
          }
          saveData.vehicleId = this.vehicleData.vehicleId;
          saveData.reportId = this.vehicleData.reportId;
          saveData.dealerCode = this.carData.dealerCode;
          saveData.dealerName = this.carData.dealerName;
          if (saveData['fj'] && saveData['fj'].length > 0) {
            saveData.otherFileName = saveData['fj'][0].name;
            saveData.otherUrl = saveData['fj'][0].url;
          }
          if (type == 2) {
            saveData.operateType = 45231005;
          }
          console.log(saveData, 'saveData');
          const res = await saveSalesReturn(saveData);
          if (res.resultCode == 200) {
            this.$emit('close', false, type);
          }
        }
      }
    },
    createdFrom() {
      return [
        {
          type: 'BREAK_SPACE',
          label: '零售信息'
        },
        {
          type: 'INPUT',
          label: 'VIN',
          fieldName: 'vin',
          placeholder: '',
          disabled: true
        },
        {
          type: 'DATE',
          label: '出库日期',
          fieldName: 'stockDate',
          placeholder: '',
          disabled: true
        },
        {
          type: 'DATE',
          label: '实销日期',
          fieldName: 'reportDate',
          disabled: true,
          placeholder: ''
        },
        {
          type: 'INPUT',
          label: '产品代码',
          fieldName: 'productCode',
          disabled: true,
          placeholder: ''
        },
        {
          type: 'INPUT',
          label: '产品名称',
          fieldName: 'productName',
          disabled: true,
          placeholder: ''
        },
        {
          type: 'INPUT',
          label: '经销商',
          fieldName: 'dealerName',
          disabled: true,
          placeholder: ''
        },
        {
          type: 'INPUT',
          label: '零售单号',
          fieldName: 'orderNo',
          disabled: true,
          placeholder: ''
        },
        {
          type: 'INPUT',
          label: '客户名称',
          fieldName: 'customerName',
          disabled: true,
          placeholder: ''
        },
        {
          type: 'INPUT',
          label: '联系电话',
          fieldName: 'mobile',
          disabled: true,
          placeholder: ''
        },
        {
          type: 'SELECT',
          label: '客户类型',
          fieldName: 'customerType',
          disabled: true,
          placeholder: '',
          itemList: this.createDictList('1523')
        },
        {
          type: 'SELECT',
          label: '证件类型',
          fieldName: 'cardType',
          disabled: true,
          placeholder: '',
          itemList: this.createDictList('1508')
        },
        {
          type: 'INPUT',
          label: '证件号',
          fieldName: 'cardNo',
          disabled: true,
          placeholder: ''
        },
        {
          type: 'SELECT',
          label: '性别',
          fieldName: 'sex',
          disabled: true,
          placeholder: '',
          itemList: this.createDictList('1002')
        },
        {
          type: 'INPUT',
          label: '省份',
          fieldName: 'provinceName',
          disabled: true,
          placeholder: ''
        },
        {
          type: 'INPUT',
          label: '城市',
          fieldName: 'cityName',
          disabled: true,
          placeholder: ''
        },
        {
          type: 'TEXT_AREA',
          label: '详细地址',
          fieldName: 'address',
          placeholder: '',
          disabled: true,
          selfCols: 3
        },
        {
          type: 'BREAK_SPACE',
          label: '退车单信息'
        },
        {
          type: 'SELECT',
          label: '是否质量问题',
          fieldName: 'isQualityProblem',
          placeholder: '请选择',
          itemList: this.createDictList('4521'),
          offsetRightCols: 1,
          disabled: true
        },
        {
          type: 'SELECT',
          label: '退车类型',
          fieldName: 'returnType',
          placeholder: '请选择',
          itemList: this.createDictList('4522'),
          disabled: true
        },
        {
          type: 'TEXT_AREA',
          label: '退车说明',
          fieldName: 'returnDesc',
          placeholder: '请输入',
          selfCols: 3,
          disabled: true
        },
        {
          type: 'SELECT',
          label: '仓库',
          fieldName: 'returnWarehouse',
          placeholder: '请选择',
          itemList: [],
          rules: [{ required: true, message: '请选择', trigger: 'change' }]
        },
        {
          type: 'INPUT',
          label: '收车人',
          fieldName: 'scr',
          initialValue: getUser(),
          placeholder: '',
          disabled: true
        },
        {
          type: 'DATE',
          label: '收车时间',
          fieldName: 'scsj',
          placeholder: '',
          initialValue: moment().format('YYYY-MM-DD'),
          dateType: 'exactdate'
        },
        {
          type: 'BREAK_SPACE',
          label: '上传附件'
        },
        {
          type: 'UPLOAD_FILE',
          label: '附件',
          fieldName: 'fj',
          placeholder: '请选择',
          initialValue: [],
          offsetRightCols: 1,
          disabled: true,
          upload: {
            actionUrl: '/api/file/oss/upload',
            limit: 1,
            fileSize: 1
          }
        }
      ];
    }
  }
};
</script>
